﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TPA.DataAccess
{
    public interface IChannelBLL
    {
        void Add(Channel c);
        void Remove(Channel c);
        void SaveChanges();
        void Refresh();
        Channel Get(string name);
        IEnumerable<Channel> GetAll();
    }
    public class ChannelBLL:IChannelBLL
    {
        TVProgamAnalyticEntities myEntities = new TVProgamAnalyticEntities();


        public void Add(Channel c)
        {
            myEntities.Channels.AddObject(c);
            //throw new NotImplementedException();
        }

        public void Remove(Channel c)
        {
            myEntities.Channels.DeleteObject(c);
            //throw new NotImplementedException();
        }

        public void SaveChanges()
        {
            myEntities.SaveChanges();
            //throw new NotImplementedException();
        }

        public void Refresh()
        {
            myEntities.Refresh(System.Data.Objects.RefreshMode.StoreWins, myEntities.Channels);
            //throw new NotImplementedException();
        }

        public Channel Get(string name)
        {
            var result = from c in myEntities.Channels
                         where c.Name == name
                         select c;
            return result.Count()==1?result.Single():null;
            //throw new NotImplementedException();
        }

        public IEnumerable<Channel> GetAll()
        {
            return (from c in myEntities.Channels select c);
            //throw new NotImplementedException();
        }
    }
}
